﻿/**
 * Created by Administrator on 2017/9/14.
 */
define(['jquery','api'],function ($,api) {
    var CustomerSearch = {
        name: 'CustomerSearch',
        template: ''
        + '<div class="company-one customer-search" id="customer-search" v-cloak>'
        + '    <div class="concluded-top-two">'
        + '        <div class="c-search-box"><span>关键字：</span><input type="text" class="c-input"  v-model="searchInfo.fuzzyKey" placeholder="输入手机、姓名、客户来源">'
        + '            <input type="button" class="c-search" value="搜索"  v-on:click="search">'
        + '        </div>'
        + '    </div>'
        + '</div>',
        props: {
            poolType: String
        },
        data: function () {
            return {
                sortBy: "CreatedTime,false",
                searchInfo: {
                    fuzzyKey: "",
                    sortBy: "",
                    pageIndex: 0,
                    pageSize: 0,
                    isAscending: "false"
                },
            }
        },
        computed: {
        },
        mounted: function () {
            var thisSearch = this;
            //监听showCustomerAdd
            window.bus.$on("showCustomerAdd", function (data) {
                thisSearch.showCustomerAdd = true;
            });
        },
        methods: {
            setSort: function () {
                var sortArr = this.sortBy.split(",");
                this.searchInfo.sortBy = sortArr[0];
                this.searchInfo.isAscending = sortArr[1];
            },
            search: function (isBack) {
                var thisSearch = this;
                thisSearch.setSort();
                api.Get_Pool_Customer_Count(thisSearch.poolType,thisSearch.searchInfo)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            //触发changePage
                            window.bus.$emit("changePage", { totalRows: data.ResultObject, isBackFirstPage: isBack });//通过 bus 触发数据总数变更事件,并跳回第一页事件.
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    })
            },
            searchNext: function (Id, isNext) {
                var thisSearch = this;
                var s = {};
                $.extend(true, s, thisSearch.searchInfo);
                s.customerId = Id;
                s.isAscending = isNext ? s.isAscending : s.isAscending === "false" ? "true" : "false";

                api.Get_Pool_Customer_Next(thisSearch.poolType,s)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            //触发showCustomerDetail
                            window.bus.$emit("showCustomerDetail", { customerId: data.ResultObject.Id });
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            },
            exportData: function () {
                var thisSearch = this;
                api.Get_Pool_Customer_Count(thisSearch.poolType,thisSearch.searchInfo)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            thisSearch.searchInfo.pageIndex = 1;
                            thisSearch.searchInfo.pageSize = data.ResultObject;
                            api.Get_Pool_Customer_List(thisSearch.poolType,thisSearch.searchInfo)
                                .done(function (data) {
                                    if (data.IsSuccess) {
                                        //触发bindExportData
                                        window.bus.$emit("bindExportData", data.ResultObject);//通过 bus 触发数据绑定事件.
                                    } else {
                                        thisSearch.$Message.error(data.ErrMsg);
                                    }
                                })
                                .fail(function () {
                                    thisSearch.$Message.error('查询出错');
                                });
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            },
        },
        created: function () {
            var thisSearch = this;
            //通过bus,监听数据查询事件,监听searchData
            window.bus.$on("searchData", function (dataPage) {
                thisSearch.searchInfo.pageIndex = dataPage.pageNum;
                thisSearch.searchInfo.pageSize = dataPage.pageSize;

                api.Get_Pool_Customer_List(thisSearch.poolType,thisSearch.searchInfo)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            //触发bindData
                            window.bus.$emit("bindData", data.ResultObject);//通过 bus 触发数据绑定事件.
                        } else {
                            thisSearch.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisSearch.$Message.error('查询出错');
                    });
            });
            //通过bus,监听查询事件，监听search
            window.bus.$on("search", function (isBack) {
                thisSearch.search(isBack);
            });

            //监听recoveredCustomerSuccess
            window.bus.$on("recoveredCustomerSuccess", function () {
                thisSearch.search(true);
            });
            //监听changeSort
            window.bus.$on("changeSort", function (sort) {
                thisSearch.sortBy = sort;
                thisSearch.search(true);
            });

            //监听afterExportData
            window.bus.$on("afterExportData", function () {
                thisSearch.search(true);
            });

            //监听nextCustomerDetail
            window.bus.$on("nextCustomerDetail", function (Id) {
                thisSearch.searchNext(Id, true);
            });

            //监听previousCustomerDetail
            window.bus.$on("previousCustomerDetail", function (Id) {
                thisSearch.searchNext(Id, false);
            });

            //监听exportData
            window.bus.$on("exportData", function (Id) {
                thisSearch.exportData();
            });
        },
    };
    return CustomerSearch;
});
